package com.ling.pl.security.view.register;

import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.core.Configure;
import com.ling.pl.security.feign.DefaultCompanyService;
import com.ling.pl.security.feign.DefaultUserService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author bo.wang
 * @since 2013-1-31
 */
@Component("ling2.registerWizard")
public class RegisterWizard {
    @Resource
    private DefaultUserService userService;
    @Resource
    private DefaultCompanyService companyService;

    public void onInit() {
        boolean disabledRegister = Configure.getBoolean("ling2.disabledSystemRegister");
        if (disabledRegister) {
            throw new RuntimeException("The current page has been disabled");
        }
    }

    @DataResolver
    public void registerCompanyAndUser(Map<String, Object> company, Map<String, Object> user) throws Exception {
        Map entity = new HashMap();
        entity.put("company", company);
        entity.put("user", user);
        this.userService.registerCompanyAndUser(entity);
    }

    @Expose
    public String checkCompanyExist(String id) {
        return companyService.checkCompanyExist(id);
    }

    @Expose
    public String checkUserExist(String username) {
        return userService.checkUserExist(username);
    }
}
